import { ChangeDetectionStrategy, Component } from '@angular/core';
import { DelonFormModule, SFSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzSpinModule } from 'ng-zorro-antd/spin';
import { Api } from 'src/app/shared/api';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-dashboard-folder',
  templateUrl: './folder.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [NzSpinModule, DelonFormModule, NzButtonModule]
})
export class FolderComponent {
  record: any = {};
  parentId: string = '0';
  schema: SFSchema = {
    properties: {
      name: {
        type: 'string',
        title: '名称'
      }
      // user: {
      //     type: 'string',
      //     title: '我',
      //     readOnly: true,
      //     enum: [{ label: '可见', value: 1 },{ label: '下载', value: 2 }],
      //     ui: {
      //         widget: 'checkbox',
      //         span: 6,
      //     } as SFCheckboxWidgetSchema,
      //     default: [1,2],
      // },
      // group: {
      //     type: 'string',
      //     title: '组',
      //     enum: [{ label: '可见', value: 1 },{ label: '下载', value: 2 }],
      //     ui: {
      //         widget: 'checkbox',
      //         span: 6,
      //         checkAll: true,
      //     } as SFCheckboxWidgetSchema,
      // },
      // other: {
      //     type: 'string',
      //     title: '其他',
      //     enum: [{ label: '可见', value: 1 },{ label: '下载', value: 2 }],
      //     ui: {
      //         widget: 'checkbox',
      //         span: 6,
      //         checkAll: true,
      //     } as SFCheckboxWidgetSchema,
      // },
    }
  };
  ui: SFUISchema = {
    '*': {
      spanLabel: 2,
      spanControl: 22
    }
  };

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient
  ) {
    const config = this.modal.getConfig();
    if (config && config.nzData) {
      this.parentId = config.nzData?.parentId;
      if (!this.parentId) {
        this.parentId = '0';
      }
    }
  }

  close() {
    this.modal.destroy();
  }

  save(value: any) {
    console.log(value);
    let entity: any = { name: value.name, userAuthority: 0, groupAuthority: 0, otherAuthority: 0 };
    if (value.user) {
      value.user.forEach((element: any) => {
        entity.userAuthority += element;
      });
    } else {
      entity.userAuthority = 7;
    }
    if (value.group) {
      value.group.forEach((element: any) => {
        entity.groupAuthority += element;
      });
    }
    if (value.other) {
      value.other.forEach((element: any) => {
        entity.otherAuthority += element;
      });
    }
    if (this.record.id) {
      delete entity.parentId;
      this.http.put(`${Api.BaseNodeApi}/folder`, entity).subscribe(res => {
        if (res.code === ResponseCode.SUCCESS) {
          this.msgSrv.success('成功！');
          this.modal.close(true);
        } else {
          this.msgSrv.error(res.message ? res.message : '失败，未知原因！');
        }
      });
    } else {
      entity.parentId = this.parentId;
      this.http.post(`${Api.BaseNodeApi}/folder`, entity).subscribe(res => {
        if (res.code === ResponseCode.SUCCESS) {
          this.msgSrv.success('成功！');
          this.modal.close(true);
        } else {
          this.msgSrv.error(res.message ? res.message : '失败，未知原因！');
        }
      });
    }
  }
}
